const path = require('path');
require('dotenv').config();

/**
 * 应用程序配置
 * 从环境变量加载配置信息
 */
const config = {
  // 服务器配置
  server: {
    port: process.env.PORT || 3000,
    wsPort: process.env.WS_PORT || 9000,
    env: process.env.NODE_ENV || 'development',
  },

  // 腾讯云API配置
  tencentCloud: {
    secretId: process.env.TENCENT_SECRET_ID,
    secretKey: process.env.TENCENT_SECRET_KEY,
    region: process.env.TENCENT_REGION || 'ap-guangzhou',
  },

  // 语音识别配置
  asr: {
    engineType: process.env.ASR_ENGINE_TYPE || '16k_zh',
  },

  // 文本转语音配置
  tts: {
    voiceType: parseInt(process.env.TTS_VOICE_TYPE || '0'),
    volume: parseInt(process.env.TTS_VOLUME || '5'),
    speed: parseInt(process.env.TTS_SPEED || '0'),
    projectId: parseInt(process.env.TTS_PROJECT_ID || '0'),
    modelType: parseInt(process.env.TTS_MODEL_TYPE || '1'),
  },

  // 天气API配置
  weather: {
    apiKey: process.env.WEATHER_API_KEY,
    apiUrl: process.env.WEATHER_API_URL || 'https://api.openweathermap.org/data/2.5',
    units: process.env.WEATHER_UNITS || 'metric',
    lang: process.env.WEATHER_LANG || 'zh_cn',
  },

  // 日志配置
  logger: {
    level: process.env.LOG_LEVEL || 'info',
  },

  // 数据文件路径
  dataFiles: {
    chat: process.env.CHAT_DATA_FILE || 'data/chat.json',
    family: process.env.FAMILY_DATA_FILE || 'data/family.json',
    weather: process.env.WEATHER_DATA_FILE || 'data/weather.json',
  },

  // CORS配置
  cors: {
    origin: process.env.CORS_ORIGIN || '*',
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Authorization'],
  },

  // 上传文件配置
  upload: {
    maxSize: parseInt(process.env.UPLOAD_MAX_SIZE || '10') * 1024 * 1024, // 默认10MB
    destination: path.join(__dirname, 'uploads'),
    allowedTypes: [
      'image/jpeg',
      'image/png',
      'image/gif',
      'audio/wav',
      'audio/mp3',
      'audio/mpeg',
      'audio/ogg',
    ],
  },

  // 路由前缀
  apiPrefix: '/api',

  // 静态文件配置
  static: {
    path: path.join(__dirname, '../frontend/dist'),
    options: {
      maxAge: '1d',
    },
  },

  // 会话配置
  session: {
    secret: process.env.SESSION_SECRET || 'ctrl100panel-secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
      secure: process.env.NODE_ENV === 'production',
      maxAge: 24 * 60 * 60 * 1000, // 1天
    },
  },

  // 安全配置
  security: {
    helmet: {
      contentSecurityPolicy: {
        directives: {
          defaultSrc: ["'self'"],
          scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
          styleSrc: ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com'],
          fontSrc: ["'self'", 'https://fonts.gstatic.com'],
          imgSrc: ["'self'", 'data:', 'https://openweathermap.org'],
          connectSrc: ["'self'", 'https://api.openweathermap.org'],
        },
      },
    },
  },

  // 压缩配置
  compression: {
    level: 6,
    threshold: 1024, // 1KB
  },
};

module.exports = config;